﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Krile.Kernel.Data;
using Krile.Kernel.Control.Surface;
using Krile.Forms.MainForm;
using Krile.Kernel.Bridges;
using System.Drawing;

namespace Krile.Data
{
    internal static class DefaultCommandButtons
    {
        internal static CommandButton GetFavButton(Status target)
        {
            CommandButtonBasicImpl fav = new FavButton(target, Lang.DefaultCommandButton.SetFavor);
            fav.OnMouseClicked += new Action<ChainMouseEventArgs>((e) =>
            {
                Action<IForm> act = new Action<IForm>((f) =>
                {
                    fav.Enabled = false;
                    Core.FormManager.MainForm.SetStatusText(Lang.StatusText.Favouring);
                    f.RefreshTL(false);
                    var or = target.ParentAgent.Status.SetFavorite(target.UID, !target.Favored);
                    if (or.Succeeded)
                    {
                        target.Favored = !target.Favored;
                    }
                    else
                    {
                        Subsystem.Debugger.AddReport(new Krile.Kernel.Bridges.DebugReport("Fav operation was failed at " + target.UID.ToString()));
                        Core.FormManager.MainForm.SetStatusText(Lang.StatusText.FavError, 3);
                    }
                    fav.Enabled = true;
                    f.RefreshTL(true);
                    Core.FormManager.MainForm.SetStatusText(Lang.StatusText.Favored, 3);
                    Core.StatusManager.UpdateStatus(new[] { target.UID });
                });
                act.BeginInvoke(e.ParentForm, (iar) => ((Action<IForm>)iar.AsyncState).EndInvoke(iar), act);
            });
            return fav;
        }

        internal static CommandButton GetReplyButton(Status target)
        {
            var rep = new CommandButtonBasicImpl(Core.GlobalImage.ReplyImage, Lang.DefaultCommandButton.SetReply);
            rep.OnMouseClicked += new Action<ChainMouseEventArgs>((e) => Core.FormManager.MainForm.InputControlManager.SetReplyTo(target.UID));
            return rep;
        }

        internal static CommandButton GetDeleteButton(Status target)
        {
            var del = new CommandButtonBasicImpl(Core.GlobalImage.DeleteImage, Lang.DefaultCommandButton.Delete);
            del.OnMouseClicked += new Action<ChainMouseEventArgs>((e) => Core.StatusManager.RemoveStatusWithOnSvr(target));
            return del;
        }
    }

    internal class FavButton : CommandButtonBasicImpl
    {
        static Image GrayedStar = Properties.Resources.grayed_star;
        static Image Star = Properties.Resources.star;

        Status parent;
        internal FavButton(Status parent, String desc)
            : base(null, desc)
        { 
        this.parent = parent;
        }

        public override bool Checked { get { return parent.Favored; } set { } }

        public override System.Drawing.Image IconImage
        {
            get { return parent.Favored ? Star : GrayedStar; }
            set { }
        }
    }
}
